<?php

namespace App\Http\Controllers\Dawn;

use App\Http\Controllers\Controller;
use App\Model\Friend;
use Illuminate\Http\Request;

class FriendController extends Controller
{
    //
    public function getFriendList(Request $request)
    {
        $user_id = $request->get('user_id');
        if(empty($user_id)){
            $arr = [
                'code'=>'4001',
                'codeinfo'=>'未获取用户id',
                'data'=>null
            ];
            return json_encode($arr);
        }
        $friend_list = Friend::where('user_id', $user_id)->get()->toArray();
        if(empty($friend_list)){
            $arr = [
                'code'=>'4001',
                'codeinfo'=>'用户好友为空',
                'data'=>null
            ];
            return json_encode($arr);
        }else{
            $arr = [
                'code'=>'0000',
                'codeinfo'=>'获取成功',
                'data'=>$friend_list
            ];
            return json_encode($arr);
        }
    }
    public function addFriendList(Request $request)
    {
        $data = $request->all();
        //dd($data['user_id']);
        if(empty($data)){
            $arr = [
                'code'=>'4001',
                'codeinfo'=>'未获取数据',
                'data'=>null
            ];
            return json_encode($arr);
        }
        $friend = new Friend();
        $friend->user_id = $data['user_id'];
        $friend->friend_id = $data['friend_id'];
        $friend->name = $data['name'];
        $friend->number = $data['number'];
        $friend->save();
        //$id = $friend->id;
        $arr = [
            'code'=>'0000',
            'codeinfo'=>'添加成功',
            'data'=>null
        ];
        return json_encode($arr);
    }
}
